"""The application's model objects"""
import sqlalchemy as sa
from sqlalchemy import types, update
from pylons import session
from pylons.controllers.util import abort, redirect
import logging
from sqlalchemy.orm import query
log = logging.getLogger(__name__)

from portal.model.meta import Session, Base
import datetime

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    Session.configure(bind=engine)


def empty_database():
    log.info("Deteling Database cc")
    meta.Session.execute("DROP DATABASE kenshuri_cc;")
    meta.Session.commit()
    log.info("Database cc dropped")
    log.info("Creating database cc")
    meta.Session.execute("CREATE DATABASE kenshuri_cc;")
    meta.Session.commit()
    meta.Session.execute("use kenshuri_cc;")
    meta.Session.commit()
    log.info("Database cc created")
    return True
    

class User(Base):
    __tablename__ = "user"
    
    id = sa.Column(types.Integer, primary_key=True)
    email = sa.Column(types.Unicode(255))
    password = sa.Column(types.Unicode(64))
    first_name = sa.Column(types.Unicode(255))
    last_name = sa.Column(types.Unicode(255))
    when_created = sa.Column(types.DateTime())
    last_login = sa.Column(types.DateTime())

    
    def create_default_user_admin(self):
        admin = User()
        admin.when_created = datetime.datetime.now()
        admin.email = 'admin@admin.com'
        admin.password = 'admin'
        admin.last_name = 'admin'
        admin.first_name = 'admin'
    
        Session.add(admin)
        Session.commit()

class News(Base):
    __tablename__ = "news"
    
    id = sa.Column(types.Integer, primary_key=True)
    title = sa.Column(types.Unicode(255))
    content = sa.Column(types.Text)
    us_id = sa.Column(types.Integer)
    when_created = sa.Column(types.DateTime())
    
    def create_default_news(self):
        news = News()
        news.when_created = datetime.datetime.now()
        news.title = 'Bienvenue !'
        news.content = 'Bienvenue sur le site de la compagnie chrysalide'
            
        Session.add(news)
        Session.commit()
        
    def add_news(self, title, content):
        news = News()
        news.when_created = datetime.datetime.now()
        news.title = title
        news.content = content
        news.us_id = session['user']
        Session.add(news)
        Session.commit()
    
    def update_news(self,id, title, content):
        Session.query(News).filter_by(id = id).update({ 'title': title, 'content': content })
    
    
class Spectacle(Base):
    
    __tablename__ = "spectacle"
    
    id = sa.Column(types.Integer, primary_key=True)
    title = sa.Column(types.Unicode(255))
    content = sa.Column(types.Text)
    when_created = sa.Column(types.DateTime())
    md5 = sa.Column(types.Unicode(32))
    us_id = sa.Column(types.Integer)
    
    
    def add_spectacle(self, title, content):
        spectacle = Spectacle()
        spectacle.when_created = datetime.datetime.now()
        spectacle.title = title
        spectacle.content = content
        log.debug(session['user'])
        #spectacle.us_id = session['user']
        Session.add(spectacle)
        Session.commit()
        
    def update_spectacle(self, id, title, content):
        Session.query(Spectacle).filter_by(id = id).update({'title': title, 'content': content })
        
        
    
class Page(Base):
    __tablename__ = "page"
    id = sa.Column(types.Integer, primary_key=True)
    name = sa.Column(types.Unicode(255))
    content = sa.Column(types.Text)
    
    def update_page(self,id, content):
        Session.query(Page).filter_by(id = id).update({ 'content': content })
            
            
class Evenement(Base):
    __tablename__ = "evenement"
    id = sa.Column(types.Integer, primary_key=True)
    name = sa.Column(types.Unicode(255))
    content = sa.Column(types.Text)
    us_id = sa.Column(types.Integer)
    when_created = sa.Column(types.DateTime())
    
    def add_evenement(self, name, content):
        evenement = Evenement()
        evenement.when_created = datetime.datetime.now()
        evenement.name = name
        evenement.content = content
        evenement.us_id = session['user']
        
        Session.add(evenement)
        Session.commit()
    
    def update_evenement(self, id, name, content):
        Session.query(Evenement).filter_by(id = id).update({'name': name, 'content': content })
        
class Programmation(Base):
    __tablename__ = "programmation"
    id = sa.Column(types.Integer, primary_key=True)
    title = sa.Column(types.Unicode(255))
    content = sa.Column(types.Text)
    us_id = sa.Column(types.Integer)
    when_created = sa.Column(types.DateTime())
    
    def add_programmation(self, title, content):
        programmation =Programmation()
        programmation.when_created = datetime.datetime.now()
        programmation.title = title
        programmation.content = content
        #evenement.us_id = session['user']
        
        Session.add(programmation)
        Session.commit()
    
    def update_programmation(self, id, title, content):
        Session.query(Programmation).filter_by(id = id).update({'title': title, 'content': content })